package com.lrd.snapshotorder.util;

public class ShardingIdUtils {
    public ShardingIdUtils() {
    }

    public static int getNumByLength(int length) {
        if (length < 10 && length >= 1) {
            int mid = (int) Math.pow(10.0D, (double) (length - 1));
            return (int) (Math.random() * 9.0D * (double) mid) + mid;
        } else {
            throw new RuntimeException("最大支持1-9位长度的随机数生成");
        }
    }

    public static String getLastNumber(String idStr, int length) {
        int size = Math.min(idStr.length(), length);
        idStr = idStr.substring(idStr.length() - size);
        return String.format("%0" + length + "d", Long.valueOf(idStr));
    }

    public static String getFirstNumber(String idStr, int length) {
        if (length > idStr.length()) {
            throw new RuntimeException("截取长度超过总长度");
        } else {
            idStr = idStr.substring(0, length);
            return String.format("%0" + length + "d", Long.valueOf(idStr));
        }
    }

}